package com.woniu108.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.car.param.*;
import com.woniu108.carService.commos.dto.AdminInfoDto;
import com.woniu108.carService.commos.exception.admin.AdminException;
import com.woniu108.carService.commos.exception.admin.AdminExceptionCode;
import com.woniu108.carService.commos.exception.car.CarException;
import com.woniu108.carService.commos.exception.car.CarExceptionCode;
import com.woniu108.carService.commos.util.StaticData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.woniu108.car.client.PersonClient;
import com.woniu108.car.mapper.CarSeriesMapper;
import com.woniu108.car.mapper.CarStyleMapper;
import com.woniu108.car.model.CarSeries;
import com.woniu108.car.model.CarStyle;
import com.woniu108.car.service.CarStyleService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 车辆款式表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class CarStyleServiceImpl extends ServiceImpl<CarStyleMapper, CarStyle> implements CarStyleService {
    @Resource
    private PersonClient personClient;
    @Resource
    private CarStyleMapper carStyleMapper;

    @Resource
    private CarSeriesMapper carSeriesMapper;

    /**
     * 判断管理员
     */
    private void isAdmin(String adminId) {
        // 检查 adminId 是否为 null
        if (adminId == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_ID_NULL);
        }
        // 判断管理员权限
        AdminInfoDto adminInfo = personClient.getByAdminId(adminId).getData();
        if (adminInfo==null){
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_HAVE);
        }
        if (adminInfo.getAdminRoleId() != 1) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_PERMISSION);
        }
    }

    @Override
    public List<CarStyle> findAllCarStyle(Integer seriesId) {
        QueryWrapper<CarStyle> wrapper = new QueryWrapper<>();
        wrapper.eq("series_id", seriesId)
                .ne("style_state", StaticData.CAR_STYLE_STATE_DELETE);
        return carStyleMapper.selectList(wrapper);
    }

    @Override
    public List<CarStyle> findAllNormalCarStyle(Integer seriesId) {
        QueryWrapper<CarStyle> wrapper = new QueryWrapper<>();
        wrapper.eq("series_id", seriesId)
                .eq("style_state", StaticData.CAR_STYLE_STATE_NORMAL);
        return carStyleMapper.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertCarStyle(InsertCarStyleParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        //查数据库中是否已存在该款式
        QueryWrapper<CarStyle> wrapper = new QueryWrapper<>();
        wrapper.eq("series_id", param.getSeriesId())
                .eq("style_name", param.getStyleName());
        CarStyle selectOne = carStyleMapper.selectOne(wrapper);
        //判断是否已存在该款式
        if (selectOne != null) {
            //存在该款式，判断款式状态
            //正常或被禁用
            if (selectOne.getStyleState().equals(StaticData.CAR_STYLE_STATE_NORMAL) || selectOne.getStyleState().equals(StaticData.CAR_STYLE_STATE_BAN)) {
                throw new CarException(CarExceptionCode.CAR_STYLE_HAVE);
            }
            //若是被删除状态，则改为正常
            selectOne.setStyleState(StaticData.CAR_STYLE_STATE_NORMAL);
            carStyleMapper.updateById(selectOne);
        } else {
            //不存在该款式，新增款式
            //查该系列品牌ID
            CarSeries carSeries = carSeriesMapper.selectById(param.getSeriesId());
            //新增
            CarStyle carStyle = new CarStyle();
            carStyle.setBrandId(carSeries.getBrandId());
            carStyle.setSeriesId(param.getSeriesId());
            carStyle.setCSId(param.getCSId());
            carStyle.setStyleName(param.getStyleName());
            carStyle.setStyleDetails(param.getStyleDetails());
            carStyle.setCarPrice(param.getCarPrice());
            carStyle.setStyleState(StaticData.CAR_STYLE_STATE_NORMAL);
            carStyleMapper.insert(carStyle);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteCarStyle(DeleteCarStyleParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        for (Integer styleId : param.getStyleIds()) {
            //删除汽车款式
            UpdateWrapper<CarStyle> styleUpdateWrapper = new UpdateWrapper<>();
            styleUpdateWrapper.set("style_state", StaticData.CAR_STYLE_STATE_DELETE);
            styleUpdateWrapper.eq("style_id", styleId);
            int styleUpdate = carStyleMapper.update(null, styleUpdateWrapper);
            if (styleUpdate <= 0) {
                throw new CarException(CarExceptionCode.CAR_STYLE_NOT_HAVE);
            }

        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean banCarStyle(BanCarStyleParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        for (Integer styleId : param.getStyleIds()) {

            //禁用汽车款式
            CarStyle carStyle = new CarStyle();
            carStyle.setStyleId(styleId);
            carStyle.setStyleState(StaticData.CAR_STYLE_STATE_BAN);
            CarStyle style = carStyleMapper.selectById(styleId);
            if (Objects.equals(style.getStyleState(), StaticData.CAR_STYLE_STATE_DELETE)) {
                throw new CarException(CarExceptionCode.CAR_STYLE_STATE_DELETE);
            }
            if (Objects.equals(style.getStyleState(), StaticData.CAR_STYLE_STATE_BAN)) {
                throw new CarException(CarExceptionCode.CAR_STYLE_STATE_BAN);
            }
            int update = carStyleMapper.updateById(carStyle);
            if (update <= 0) {
                throw new CarException(CarExceptionCode.CAR_STYLE_NOT_HAVE);
            }

        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean unlockCarStyle(UnlockCarStyleParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        for (Integer styleId : param.getStyleIds()) {

            //解锁汽车款式
            CarStyle carStyle = new CarStyle();
            carStyle.setStyleId(styleId);
            carStyle.setStyleState(StaticData.CAR_STYLE_STATE_NORMAL);
            //查看当前id款式的状态
            CarStyle style = carStyleMapper.selectById(styleId);
            if (Objects.equals(style.getStyleState(), StaticData.CAR_STYLE_STATE_DELETE)) {
                throw new CarException(CarExceptionCode.CAR_STYLE_STATE_DELETE);
            }
            if (Objects.equals(style.getStyleState(), StaticData.CAR_STYLE_STATE_NORMAL)) {
                throw new CarException(CarExceptionCode.CAR_STYLE_STATE_NORMAL);
            }
            int update = carStyleMapper.updateById(carStyle);
            if (update <= 0) {
                throw new CarException(CarExceptionCode.CAR_STYLE_NOT_HAVE);
            }


        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateCarStyle(UpdateCarStyleParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        CarStyle carStyle = carStyleMapper.selectById(param.getStyleId());

        if (carStyle == null) {
            throw new CarException(CarExceptionCode.CAR_STYLE_NOT_HAVE);
        }
        if (Objects.equals(carStyle.getStyleState(), StaticData.CAR_STYLE_STATE_DELETE)) {
            throw new CarException(CarExceptionCode.CAR_STYLE_STATE_DELETE);
        }
        if (param.getStyleName() != null) {
            carStyle.setStyleName(param.getStyleName());
        }
        if (param.getStyleDetails() != null) {
            carStyle.setStyleDetails(param.getStyleDetails());
        }
        if (param.getCarPrice() != null) {
            carStyle.setCarPrice(param.getCarPrice());
        }
        carStyleMapper.updateById(carStyle);
        return true;
    }

}
